<?php
/**
 * 《员工薪金》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Salary extends FinanceUtil {
	public $tbl = Constant::tbl_cw_salary;
	
	public $payment = array(
		'-' => '请选择',
		'1' => '现金',		
		'2' => '转帐',
		
		'9' => '其它',
	);
	
	public $type = array(
		'-' => '请选择',
		'1' => '每月工资',
		'2' => '离职工资',
		'3' => '年终奖金',
		// '4' => '奖金',
		// '3' => '补助',

		'9' => '其它',
	);
	
	public $amount = array(
		'-' => '请选择',
		'1' => '对私',		
		'2' => '对公',
		
		'9' => '其它',
	);
	
	public $dfgz_bank = array(
		'-' => '请选择',
		'1' => '中国工商银行',
		'2' => '中信银行',
		'3' => '东莞农村商业银行',
		'4' => '中国民生银行',

		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待发放',
		'3' => '正在发放',
		'4' => '完成发放',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
	
		'dept, id' => '部门',
			
		'd2' => '出勤',
		
		't1' => '应发金额',
		't2' => '实发金额',
	
		'ns' => '对私',
		'sj' => '对公',
		'finish' => '完成时间',
	
		'status' => '状态',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
			
		'office' => '办公区',
		'name' => '名称',
		'emp' => '员工',
		'office' => '办公区',
		'dept' => '部门',
	
		'status' => '状态',	
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('amount_option', $this->amount);
		$controller->assign_by_ref('dfgz_bank', $this->dfgz_bank);
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {
		$data = array(
			'批量导入' => array('cc'=>'finance.php?do=SalaryFile.find', 'js'=>''),
			'每月工资' => array('cc'=>'finance.php?do=Salary.find', 'js'=>''),
			'工资信息' => array('cc'=>'emp.php?do=Salary.find', 'js'=>'1'),
			'社保信息' => array('cc'=>'finance.php?do=Social.find', 'js'=>'1'),
			// '住房公积金' => array('cc'=>'emp.php?do=Salary.find', 'js'=>'1'),
				
			'请假信息' => array('cc'=>'kq.php?do=Vacation.find', 'js'=>'1'),
			'打卡明细' => array('cc'=>'kq.php?do=Record.find', 'js'=>'1'),
		
		    '提成信息' => array('cc'=>'finance.php?do=Commission.find', 'js'=>''),
			'收入明细' => array('cc'=>'finance.php?do=InItem.find', 'js'=>''),
			'支出明细' => array('cc'=>'finance.php?do=OutItem.find', 'js'=>''),
			
			'银行帐号' => array('cc'=>'finance.php?do=Bank.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'finance.php?do=Salary.tj_tab', 'js'=>''),
		);

		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'工资信息' => array('cc'=>'finance.php?do=Salary.group', 'js'=>''),
			'提成信息' => array('cc'=>'finance.php?do=Commission.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$dd = date("Y-m-01");

		$controller->assign('t1', strtotime($dd));
		// $controller->assign('pagesize_s', '100');
		
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'finish') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['finish'];
		}
		
		if ($px == 'emp') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['emp'];
		}
		
		if ($px == 'dept, id') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $this->_dept($row['dept']);
		}
		
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
		
		$file_s = $request['file_s'];
		
		$payment_s = $request['payment_s'];
		
		$dfgz_s = $request['dfgz_s'];

		$amount_s = $request['amount_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($payment_s) && $payment_s <> '-') {
			$where .= " AND payment = '" . $payment_s . "'";
		}
		
		if (!empty($dfgz_s) && $dfgz_s <> '-') {
			$where .= " AND dfgz = '" . $dfgz_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($file_s)) {
			$where .= " AND file = '" . $file_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND finish >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND finish < '" . $date2_s . "'";
		}
		
		if ($amount_s == '1') {
			$where .= " AND ns > 0";
		}

		if ($amount_s == '2') {
			$where .= " AND sj > 0";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		$emp = $_SESSION['id'];
		
		$p = array('1'=>'159', '2'=>'160', '3'=>'175');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
		
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
		
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/salary/list');
		return true;
	}
	
	function history($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		
		$row = $this->id($this->tbl, $id);
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND type = '%s' ORDER BY id DESC LIMIT 12";
		$sql = sprintf($fmt, $this->tbl, $row['emp'], $row['type']);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$ret[] = $row;
		}
		
		$fmt = "SELECT avg(d2) d2, avg(t1) t1, avg(t2) t2, avg(ns) ns, avg(sj) sj FROM %s WHERE emp = '%s' AND createtime > '%s' AND createtime < '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $dd, $row['createtime']);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		
		$row['name'] = '平均工资';
		$ret[] = $row;
	
		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'finance/salary/history');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";
		
		$xls = $request['xls'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s LIMIT 500";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);			
			
			if (($xls == '2' || $request['xls'] == '3') && $row['sj'] == 0) {
				continue;
			}

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		
		if ($xls == '2') {
			$controller->display($request, 'finance/salary/list_2');
		} elseif ($xls == '3') {
			$controller->display($request, 'finance/salary/list_3');
		} elseif ($xls == '4') {
			$controller->display($request, 'finance/salary/list_4');
		} else {
			$controller->display($request, 'finance/salary/list');
		}
		
		return true;
	}
	
	function xls_cgb($controller, $request) {
		$this->pretreat($controller);
	
		$request['tpl'] = "xls";
	
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
			$this->findPost($controller, $request);
			return false;
		}
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
	
		$count = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			
			if ($row['ns'] == 0) {
				continue;
			}
			
			if ($row['payment'] != '2') {
				continue;
			}
				
			$row['bank'] = $this->_bank("1", $row['emp'], $row['ns_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
				
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}
				
			$row = $this->_view(null, $row);			
			debug(print_r($row, true));
			$data[] = $row;
				
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 1, '广发银行'));
	
		$filename = sprintf("cgb_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'finance/salary/list_cgb');
		return true;
	}
	
	function xls_drc($controller, $request) {
		$this->pretreat($controller);
	
		$request['tpl'] = "xls";
	
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
			$this->findPost($controller, $request);
			return false;
		}
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
	
		$count = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
				
			if ($row['sj'] == 0) {
				continue;
			}
				
			if ($row['payment'] != '2') {
				continue;
			}
	
			$row['bank'] = $this->_bank("1", $row['emp'], $row['sj_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
			
			/*
	
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}*/
	
			$row = $this->_view(null, $row);
			debug(print_r($row, true));
			$data[] = $row;
	
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 2, '东莞农村商业银行'));
	
		$filename = sprintf("drc_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'finance/salary/list_drc');
		return true;
	}
	
	function xls_icbc($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s LIMIT 500";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("icbc_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		// $controller->display($request, 'finance/salary/list_icbc');
		$controller->display($request, 'finance/salary/list_icbc_3');
		return true;
	}

	function txt($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$bank = $request['bank'];
		
		if ($bank == 'icbc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 1, '中国工商银行');
				
			$my_tpl = 'finance/salary/icbc2';
			$filename = sprintf("%s.gbpt", date("YmdHis"));
		} elseif ($bank == 'citic') {
			$my_bank = $this->_my_bank($_SESSION['id'], 1, '中信银行');
				
			$my_tpl = 'finance/salary/citic';
			$filename = sprintf("citic-%s.txt", date("YmdHis"));
		} elseif ($bank == 'cmbc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 1, '中国民生银行', '2');
				
			$my_tpl = 'finance/salary/cmbc';
			$filename = sprintf("cmbc-%s.txt", date("YmdHis"));
		} else {
			$my_bank = $this->_my_bank($_SESSION['id'], 1, '招商银行');
				
			$my_tpl = 'finance/salary/list';
			$filename = sprintf("BATCH-%s.PAY", date("YmdHis"));
		}

		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		$count = 0;
		$total = 0;
		while ($row = $db->fetch_array($result)) {
			
			// $row['bank'] = $this->_bank("1", $row['emp'], $row['ns_x']);
			
			if ($bank == 'citic' || $bank == 'cmbc') {
				$row['bank'] = $this->_bank("1", $row['emp'], $row['sj_x']);
				
				$row = $this->_view(null, $row);
			} else {
				$row['bank'] = $this->_bank("1", $row['emp'], $row['ns_x']);
	
				$row = $this->_view(null, $row);
				$row['transmit'] = $this->_transmit_type($row['bank'], $my_bank['zone'], $my_bank['city']);
			}
			
			if (!is_array($row['bank'])) {
				continue;
			}

			$total += $row['sj'];
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		if ($bank == 'icbc' || $bank == 'cmbc') {
			$controller->assign('total', $total);
			$controller->assign('count', count($data));
		}

		debug(print_r($my_bank, true));
		
		$controller->assign_by_ref('bank', $my_bank);
		$content = $controller->sunxy($request, $my_tpl);
		
		$content = iconv("UTF-8", "GBK", trim($content));
		header('Content-Disposition:text/plain; filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');
		return true;
	}

	function icbc($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}

		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		$count = 0;
		$total = 0;
		while ($row = $db->fetch_array($result)) {
			if ($row['sj'] == 0) {
				continue;
			}

			$row['bank'] = $this->_bank("1", $row['emp'], $row['sj_x']);
			if (!is_array($row['bank'])) {
				continue;
			}

			$row['transmit'] = $this->_transmit_type($row['bank']);

			$data[] = $row;

			$total += $row['sj'];
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 1));

		$controller->assign('total', $total);
		$controller->assign('count', $count);

		$content = $controller->sunxy($request, 'finance/salary/icbc');
		$content = iconv("UTF-8", "GBK", $content);
		
		$filename = sprintf("%s.bpt", date("YmdHi"));
		header('Content-Disposition:text/vcf; filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');
		return true;
	}

	function _batch_insql($request) {
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";

		return $insql;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/add');
		return true;
	}

	function _name_option() {
		$t1 = strtotime("-13 month");
		$t2 = time();

		$arr = array('-'=>'请选择');
		while ($t1 < $t2) {
			$str = date("Y年m月工资", $t2);
			$t2 = strtotime("-1 month", $t2);

			$arr[$str] = $str;
		}
		
		$a = array("春节", "端午节", "中秋节");
		while (list($k, $v) = each($a)) {
			$str = sprintf("%s%s", date("Y年奖金"), $v);
			$arr[$str] = $str;
		}		

		return $arr;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = $request['name'];
		$type = $request['type'];
		
		$payment = $request['payment'];
		$content = addslashes($request['content']);

		$d1 = $request['d1'];
		$d2 = $request['d2'];
		$d3 = $request['d3'];
		$d4 = $request['d4'];
		$d5 = $request['d5'];

		$s1 = $request['s1'];
		$s2 = $request['s2'];
		$s3 = $request['s3'];
		$s4 = $request['s4'];

		$k1 = $request['k1'];
		$k2 = $request['k2'];
		$k9 = $request['k9'];

		$kd = $request['kd'];
		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$ns = $request['ns'];
		$sj = $request['sj'];

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, name, type, payment, d1, d2, d3, d4, d5, s1, s2, s3, s4, k1, k2, k9, t1, kd, t2, ns, sj, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $type, $payment, $d1, $d2, $d3, $d4, $d5, $s1, $s2, $s3, $s4, $k1, $k2, $k9, $t1, $kd, $t2, $ns, $sj, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _bank_null($bank) {
		if (empty($bank)) {
			return true;
		}
		
		$bank = stripslashes($bank);
		
		$json = json_decode($bank);
		if (empty($json->name) || empty($json->code)) {
			return true;
		}
		
		return false;
	}
	
	function _line_1($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/(\t|;)/", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			return false;
		}
	
		// echo $a[1] . "\n";
	
		$ret = array();
		
		$ret['emp'] = $this->get_alpha($a, 'b');   // 工号
		$ret['name'] = $this->get_alpha($a, 'c');  // 姓名
		
		$ret['d1'] = $this->get_alpha($a, 'd');  // 应出勤
		$ret['d2'] = $this->get_alpha($a, 'e');  // 实出勤
		$ret['d3'] = $this->get_alpha($a, 'f');  // 迟到/未打卡
		$ret['d4'] = $this->get_alpha($a, 'g');  // 事假
		$ret['d5'] = $this->get_alpha($a, 'i');  // 病假
	
		$ret['s1'] = $this->get_alpha($a, 'l');  // 基本工资
		$ret['s2'] = $this->get_alpha($a, 'm');  // 加班费
		$ret['s3'] = $this->get_alpha($a, 'n');  // 奖金
		$ret['s4'] = $this->get_alpha($a, 'p');  // 提成
	
		$ret['k1'] = $this->get_alpha($a, 'r');  // 社保
		$ret['k2'] = $this->get_alpha($a, 'f');  // 事病/迟到
		$ret['k9'] = $this->get_alpha($a, 'u');  // 其它
	
		$ret['t1'] = $this->get_alpha($a, 'w');  // 应发
		$ret['kd'] = $this->get_alpha($a, 'x');  // 个税
		$ret['t2'] = $this->get_alpha($a, 'y');  // 实发
		
		$ret['ns'] = $this->get_alpha($a, 'z');  // 对私
		$ret['sj'] = $this->get_alpha($a, 'aa');  // 对公
	
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
	
		return $ret;
	}

	function _line_2($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			return;
		}

		$a = preg_split("/(\t|;)/", $buf);
		$no = trim($a[0]);

		if (!preg_match("/^(\d+)$/i", $no)) {
			return;
		}

		// debug(print_r($a, true));
		// echo $a[1] . "\n";
		
		$ret = array();
		
		

		$ret['ns'] = _trim($a[10]);
		$ret['sj'] = _trim($a[12]);

		$ret['emp'] = _trim($a[15]);
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> _trim($a[1])) {
			$ret['emp'] = 0;
		}

		return $ret;
	}
	
	function _line_3($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			debug("1=" . $buf);
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}
	
		$ret = array();
		$ret['d1'] = $this->get_alpha($a, 'l');
		$ret['d2'] = $this->get_alpha($a, 'm');
		$ret['d3'] = $this->get_alpha($a, 'm');
		$ret['d4'] = $this->get_alpha($a, 'al');
	
		$ret['s1'] = $this->get_alpha($a, 'z');
		$ret['s2'] = $this->get_alpha($a, 'aa') + $this->get_alpha($a, 'ab');
		$ret['s3'] = $this->get_alpha($a, 'ac');
		$ret['s4'] = $this->get_alpha($a, 'ad');
	
		$ret['t1'] = $this->get_alpha($a, 'aj');
		
		$ret['k1'] = $this->get_alpha($a, 'ak');
		$ret['k2'] = $this->get_alpha($a, 'al');
		$ret['kd'] = $this->get_alpha($a, 'au');
		$ret['t2'] = $this->get_alpha($a, 'av');
	
		// $ns = _trim($a[45]);
		$ret['sj'] = $this->get_alpha($a, 'av');
	
		$ret['name'] = $this->get_alpha($a, 'b');
		
		$ret['emp'] = $this->get_alpha($a, 'ay');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
		
		return $ret;
	}
	
	function _line_4($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			debug("1=" . $buf);
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}

		$ret = array();
		
		$ret['d1'] = $this->get_alpha($a, 'j');
		$ret['d2'] = $this->get_alpha($a, 'k');
		// $ret['d3'] = $this->get_alpha($a, 'k');_trim($a[10]);
		$ret['d4'] = $this->get_alpha($a, 'l');
		$ret['d5'] = $this->get_alpha($a, 'm');
	
		$ret['s1'] = $this->get_alpha($a, 'u');
		$ret['s2'] = $this->get_alpha($a, 'w') + $this->get_alpha($a, 'x');
		
		$ret['s3'] = $this->get_alpha($a, 'y') + $this->get_alpha($a, 'z');
	
		$ret['t1'] = $this->get_alpha($a, 'ac');
		
		$ret['k1'] = $this->get_alpha($a, 'ad');
		$ret['kd'] = $this->get_alpha($a, 'an');
		$ret['t2'] = $this->get_alpha($a, 'ap');
	
		$ret['sj'] = $this->get_alpha($a, 'ap');
	
		$ret['name'] = $this->get_alpha($a, 'b');
		if (empty($ret['name'])) {
			return false;
		}
		
		$ret['emp'] = $this->get_alpha($a, 'as');
		
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		// if (trim($emp_x['name']) <> _trim($a[1])) {
		if (trim($emp_x['name']) <> $ret['name']) {
			$ret['emp'] = 0;
		}
	
		return $ret;
	}

	function _line_5($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			debug("1=" . $buf);
			return false;
		}

		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);

		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}

		// print_r($a);
		
		$ret = array();
		$ret['d1'] = $this->get_alpha($a, 'j');
		$ret['d2'] = $this->get_alpha($a, 'k');
		// $ret['d3'] = $this->get_alpha($a, 'c');
		$ret['d4'] = $this->get_alpha($a, 'n');
		$ret['d5'] = $this->get_alpha($a, 'o');

		$ret['s1'] = $this->get_alpha($a, 'v');
		$ret['s2'] = $this->get_alpha($a, 'x') + $this->get_alpha($a, 'y') + $this->get_alpha($a, 'z');
		$ret['s3'] = $this->get_alpha($a, 'aa');

		$ret['t1'] = $this->get_alpha($a, 'ad');
		
		$ret['k1'] = $this->get_alpha($a, 'ae');
		$ret['kd'] = $this->get_alpha($a, 'am');
		$ret['t2'] = $this->get_alpha($a, 'ao');
	
		$ret['sj'] = $this->get_alpha($a, 'ao');	

		$ret['name'] = $this->get_alpha($a, 'b');
		if (empty($ret['name'])) {
			return false;
		}
 		
		$ret['emp'] = $this->get_alpha($a, 'ar');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
		
		return $ret;
	}
	
	function _line_6($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			return false;
		}
	
		// print_r($a);
		
		$ret = array();
		$ret['d1'] = $this->get_alpha($a, 'j');
		$ret['d2'] = $this->get_alpha($a, 'k');
		$ret['d4'] = $this->get_alpha($a, 'l');
	
		$ret['s1'] = $this->get_alpha($a, 'r');
	
		$ret['t1'] = $this->get_alpha($a, 'z');
		$ret['t2'] = $this->get_alpha($a, 'am');
	
		$ret['ns'] = $this->get_alpha($a, 'am');
		
		$ret['name'] = $this->get_alpha($a, 'b');
	
		$ret['emp'] = $this->get_alpha($a, 'ap');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
	
		return $ret;
	}

	function _line_7_ns($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^(#|;)/i", $buf)) {
			return false;
		}

		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}

		// debug(print_r($a, true));
		
		$ret = array();

		$ret['ns'] = $this->get_alpha($a, 'd');
		if ($ret['ns'] <= 0) {
			return false;
		}		
		
		$ret['t1'] = $ret['ns'];
		$ret['t2'] = $ret['ns'];
		
		$ret['name'] = $this->get_alpha($a, 'c');

		$ret['emp'] = $this->get_alpha($a, 'b');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['emp']) {
			$ret['emp'] = 0;
		}

		return $ret;
	}
	
	function _line_7_sj($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^(#|;)/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}
	
		$ret = array();
	
		$ret['ns'] = $this->get_alpha($a, 'ae');
		if ($ret['ns'] <= 0) {
			return false;
		}
	
		$ret['t1'] = $ret['ns'];
		$ret['t2'] = $ret['ns'];
		
		$ret['name'] = $this->get_alpha($a, 'b');
	
		$ret['emp'] = $this->get_alpha($a, 'ah');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
		
		debug(print_r($ret, true));
	
		return $ret;
	}
	
	function _line_7_qx($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^(#|;)/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}
	
		// debug(print_r($a, true));
	
		$ret = array();
	
		$ret['ns'] = $this->get_alpha($a, 'z');
		if ($ret['ns'] <= 0) {
			return false;
		}
		
		$ret['t1'] = $ret['ns'];
		$ret['t2'] = $ret['ns'];
	
		$ret['name'] = $this->get_alpha($a, 'c');
	
		$ret['emp'] = $this->get_alpha($a, 'b');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
	
		return $ret;
	}
	
	function _line_8($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^(#|;)/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/\t/i", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			debug("3=" . $buf);
			return false;
		}
	
		$ret = array();
	
		$ret['ns'] = $this->get_alpha($a, 'e');
		if ($ret['ns'] <= 0) {
			return false;
		}
		
		$ret['t1'] = $ret['ns'];
		$ret['t2'] = $ret['ns'];
	
		$ret['name'] = $this->get_alpha($a, 'd');
	
		$ret['emp'] = $this->get_alpha($a, 'c');
		$emp_x = Common::id(Constant::tbl_emp, $ret['emp']);
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
	
		return $ret;
	}

	function _line_9($file, $name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^(#|;)/i", $buf)) {
			debug("1=" . $buf);
			return;
		}

		$a = preg_split("/;/", $buf);
		$no = trim($a[0]);
		
		if (!preg_match("/^\d+$/", $no)) {
			return;
		}

		$ns = $this->get_alpha($a, 'd');

		$emp = $this->get_alpha($a, 'b');
		if (empty($emp)) {
			debug(print_r($a, true));
			return;
		}
		
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$ns_x = $this->_payment_x($emp, '1');

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE (file = '%s' OR name = '%s') AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $file, $name, $emp);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET ns = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $ns, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(office, dept, emp, file, name, ns, ns_x, oper, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $file, $name, $ns, $ns_x, $oper);
		}
		debug($sql);
		$db->query($sql);
	}
	
	function _trim($s) {
		$s = $this->to_utf8($s);
		$s = trim($s);
		if (empty($s)) {
			return '';
		}
	
		$s = preg_replace("/[\s|\"|\'|,|-]+/i", "", $s);
	
		return $s;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		// debug(print_r($request, true));

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];
		$type = $request['type'];
		
		$payment = $request['payment'];
		$content = addslashes($request['content']);
		$status = $request['status'];

		$d1 = $request['d1'];
		$d2 = $request['d2'];
		$d3 = $request['d3'];
		$d4 = $request['d4'];
		$d5 = $request['d5'];

		$s1 = $request['s1'];
		$s2 = $request['s2'];
		$s3 = $request['s3'];
		$s4 = $request['s4'];

		$k1 = $request['k1'];
		$k2 = $request['k2'];
		$k9 = $request['k9'];

		$kd = $request['kd'];
		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$ns = $request['ns'];
		$sj = $request['sj'];

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$ns_x = addslashes($request['ns_x']);
		$this->_update_bank("1", $emp, $request['ns_x']);

		$sj_x = addslashes($request['sj_x']);
		$this->_update_bank("1", $emp, $request['sj_x']);
		
		$dfgz = $this->_dfgz(stripcslashes($sj_x));

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', name = '%s', type = '%s', payment = '%s', d1 = '%s', d2 = '%s', d3 = '%s', d4 = '%s', d5 = '%s', ";
		$fmt .= "s1 = '%s', s2 = '%s', s3 = '%s', s4 = '%s', k1 = '%s', k2 = '%s', k9 = '%s', t1 = '%s', kd = '%s', t2 = '%s', ";
		$fmt .= "ns = '%s', ns_x = '%s', sj = '%s', sj_x = '%s', dfgz = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $type, $payment, $d1, $d2, $d3, $d4, $d5, $s1, $s2, $s3, $s4, $k1, $k2, $k9, $t1, $kd, $t2, $ns, $ns_x, $sj, $sj_x, $dfgz, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _edit_file($id, $name, $type) {
		$db = newdb($this->dsn_w);
		
		$fmt = "UPDATE %s SET name = '%s', type = '%s' WHERE file = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $type, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_files_clean($id);
	
		$row = $this->_view($id);
	
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/salary/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/salary/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		// debug(print_r($row, true));
		$row = $this->_percent($row);
		// debug(print_r($row, true));
		// $row = $this->_abc($row);

		$row['payment_t'] = $this->payment[$row['payment']];
		$row['dfgz_t'] = $this->dfgz[$row['dfgz']];
		
		$row['zh_x'] = json_decode($row['ns_x']);
		$row['gh_x'] = json_decode($row['sj_x']);

		$row = $this->_view_t($row);
		
		// debug(print_r($row, true));
		
		return $row;
	}
	
	function _insert($request) {
		
		$file = $request['id'];
		$name = $request['name'];
		$type = $request['type'];
		
		$oper = $_SESSION['id'];
		
		$ret = array(
			'number' => 0,
			'amount' => 0,
		);
		
		$data = json_decode($request['item'], true);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, file, name, type, payment, d2, t2, ns, ns_x, sj, sj_x, dfgz, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				
		while (list($k, $v) = each($data)) {
				
			if (empty($v['emp']) || empty($v['t2'])) {
				continue;
			}
				
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$ns_x = $this->_payment_x($emp, '1');
			$sj_x = $this->_payment_x($emp, '2');
			
			$dfgz = $this->_dfgz(stripcslashes($sj_x));
			
			$payment = '2';
			if ($this->_bank_null($sj_x)) {
				$payment = '1';
			}
			
			$content = addslashes($v['content']);
				
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $file, $name, $type, $payment, $v['d2'], $v['t2'], $v['ns'], $ns_x, $v['sj'], $sj_x, $dfgz, $content, $oper);
			debug($sql);
			$db->query($sql);
			
			$ret['number']++;
			$ret['amount'] += $v['t2'];
		}
		
		return $ret;
	}
	
	function _edit($request) {
		// $data = $request['item'];
	
		$data = json_decode($request['item'], true);
		
		debug(print_r($data, true));
		
		$file = $request['id'];
		$name = $request['name'];
		$type = $request['type'];
	
		$oper = $_SESSION['id'];
		
		$ret = array(
			'number' => 0,
			'amount' => 0,
		);
			
		$db = newdb($this->dsn_w);
		while (list($k, $v) = each($data)) {
			// debug(print_r($v, true));
				
			if (empty($v['t2']) || empty($v['emp'])) {
				continue;
			}
				
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
				
			$ns_x = $this->_payment_x($emp, '1');
			$sj_x = $this->_payment_x($emp, '2');
				
			$dfgz = $this->_dfgz(stripcslashes($sj_x));
				
			$payment = '2';
			if ($this->_bank_null($sj_x)) {
				$payment = '1';
			}
			
			$content = addslashes($v['content']);
			
			if (array_key_exists("createtime", $v)) {
				if (!$this->_ok($v['ok'])) {
					$fmt = "UPDATE %s SET visible = '2', modifytime = now() WHERE id = '%s'";
					// $fmt = "SELECT FROM %s WHERE id = '%s'";
					$sql = sprintf($fmt, $this->tbl, $v['id']);
				} else {
					$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', name = '%s', type = '%s', payment = '%s', d2 = '%s', t2 = '%s', ns = '%s', ns_x = '%s', sj = '%s', sj_x = '%s', dfgz = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
					$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $type, $payment, $v['d2'], $v['t2'], $v['ns'], $ns_x, $v['sj'], $sj_x, $dfgz, $content, $v['id']);
					
					$ret['number']++;
					$ret['amount'] += $v['t2'];
				}
			} else {
				if (!$this->_ok($v['ok'])) {
					continue;
				}
				
				$ret['number']++;
				$ret['amount'] += $v['t2'];
				
				$fmt = "INSERT INTO %s(office, dept, emp, file, name, type, payment, d2, t2, ns, ns_x, sj, sj_x, dfgz, content, oper, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $file, $name, $type, $payment, $v['d2'], $v['t2'], $v['ns'], $ns_x, $v['sj'], $sj_x, $dfgz, $content, $oper);
			}
				
			debug($sql);
			$db->query($sql);
		}
		
		return $ret;
	}
	
	function _update($row, $data) {
		$data = json_decode($data, true);
		
		$file = $row['id'];
		$name = $row['name'];
		$type = $row['type'];
		
		$oper = $_SESSION['id'];
			
		$db = newdb($this->dsn_w);		
		while (list($k, $v) = each($data)) {
			
			if (empty($v['name']) || empty($v['emp'])) {
				continue;
			}
			
			if ($v['ok'] == '0') {
				continue;
			}
			
			$emp = $v['emp'];
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$ns_x = $this->_payment_x($emp, '1');
			$sj_x = $this->_payment_x($emp, '2');
			
			$dfgz = $this->_dfgz(stripcslashes($sj_x));
			
			$payment = '2';
			if ($this->_bank_null($sj_x)) {
				$payment = '1';
			}
			
			$fmt = "SELECT * FROM %s WHERE file = '%s' AND emp = '%s'";
			$sql = sprintf($fmt, $this->tbl, $file, $emp);
			debug($sql);
			$result = $db->query($sql);
			if ($row = $db->fetch_array($result)) {
				$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', type = '%s', payment = '%s', d1 = '%s', d2 = '%s', d3 = '%s', d4 = '%s', d5 = '%s', s1 = '%s', s2 = '%s', s3 = '%s', s4 = '%s', k1 = '%s', k2 = '%s'";
				$fmt .= ", k3 = '%s', k9 = '%s', t1 = '%s', kd = '%s', t2 = '%s', ns = '%s', ns_x = '%s', sj = '%s', sj_x = '%s', dfgz = '%s', modifytime = now() WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $type, $payment, $v['d1'], $v['d2'], $v['d3'], $v['d4'], $v['d5'], $v['s1'], $v['s2'], $v['s3'], $v['s4'], $v['k1'], $v['k2'], $v['k3'], $v['k9'], $v['t1'], $v['kd'], $v['t2'], $v['ns'], $ns_x, $v['sj'], $sj_x, $dfgz, $row['id']);
			} else {
				$fmt = "INSERT INTO %s(office, dept, file, name, type, payment, d1, d2, d3, d4, d5, s1, s2, s3, s4, k1, k2, k3, k9, t1, kd, t2, ns, ns_x, sj, sj_x, dfgz, emp, oper, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $office, $dept, $file, $name, $type, $payment, $v['d1'], $v['d2'], $v['d3'], $v['d4'], $v['d5'], $v['s1'], $v['s2'], $v['s3'], $v['s4'], $v['k1'], $v['k2'], $v['k3'], $v['k9'], $v['t1'], $v['kd'], $v['t2'], $v['ns'], $ns_x, $v['sj'], $sj_x, $dfgz, $emp, $oper);
			}
			
			debug($sql);
			$db->query($sql);
		}
	}
	
	function _abc($row) {
		$a = $this->last($row);
	
		$ret = array('d2'=>'ui-icon-arrow-1-s', 't1'=>'ui-icon-arrow-1-s','t2'=>'ui-icon-arrow-1-s','ns'=>'ui-icon-arrow-1-s','sj'=>'ui-icon-arrow-1-s');
		
		if (empty($a['d2']) || $row['d2'] == $a['d2']) {
			$ret['d2'] = 'ui-icon-arrow-1-e';
		} else if ($row['t1'] > $a['t1']) {
			$ret['d2'] = 'ui-icon-arrow-1-n';
		}
		
		if (empty($a['t1']) || $row['t1'] == $a['t1']) {
			$ret['t1'] = 'ui-icon-arrow-1-e';
		} else if ($row['t1'] > $a['t1']) {
			$ret['t1'] = 'ui-icon-arrow-1-n';
		}
	
		if (empty($a['t2']) || $row['t2'] == $a['t2']) {
			$ret['t2'] = 'ui-icon-arrow-1-e';
		} else if ($row['t2'] > $a['t2']) {
			$ret['t2'] = 'ui-icon-arrow-1-n';
		}
	
		if (empty($a['ns']) || $row['ns'] == $a['ns']) {
			$ret['ns'] = 'ui-icon-arrow-1-e';
		} else if ($row['ns'] > $a['ns']) {
			$ret['ns'] = 'ui-icon-arrow-1-n';
		}
	
		if (empty($a['sj']) || $row['sj'] == $a['sj']) {
			$ret['sj'] = 'ui-icon-arrow-1-e';
		} else if ($row['sj'] > $a['sj']) {
			$ret['sj'] = 'ui-icon-arrow-1-n';
		}
	
		// debug(print_r($a, true));
			
		$row['avg'] = $ret;
	
		return $row;
	}
	
	function _last($data) {
		$emp = $data['emp'];
		$key = sprintf("%s_last_%s_%s", $this->tbl, $data['emp'], $data['id']);
	
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
		
		$ct = strtotime($data['createtime']);
	
		$dd = date('Y-m-d', strtotime("-200 day", $ct));
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND createtime > '%s' AND createtime < '%s' ORDER BY id DESC LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $emp, $dd, $data['createtime']);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
	
			if (is_object($mc) && count($row) > 0) {
				$mc->set($key, $row, 0, _strtotime("1 day"));
			}
			
			return $row;
		}
	
		return $data;
	}
	
	function bb($controller, $request) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id = '9475'";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row = $this->percent($row);
			
			print_r($row);
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function _percent($data) {
		$emp = $data['emp'];
		/*
		$key = sprintf("%s_percent_%s_%s", $this->tbl, $data['emp'], $data['id']);
	
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}*/
	
		$ct = strtotime($data['createtime']);
	
		$dd = date('Y-m-d', strtotime("-200 day", $ct));
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND type = '%s' AND id < '%s' ORDER BY id DESC LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $emp, $data['type'], $data['id']);
		//echo $sql . "\n";
		debug($sql);
		$result = $db->query($sql);
		
		if ($row = $db->fetch_array($result)) {
			// print_r($data);
			
			if ($row['d2'] <> 0) {
				$d2 = number_format((($data['d2'] - $row['d2']) / $row['d2']) * 100, 2, '.', '') . "%";
			}
			
			if ($row['t1'] <> 0) {
				$t1 = number_format((($data['t1'] - $row['t1']) / $row['t1']) * 100, 2, '.', '') . "%";
			}
			
			if ($row['t2'] <> 0) {
				$t2 = number_format((($data['t2'] - $row['t2']) / $row['t2']) * 100, 2, '.', '') . "%";
			}
			
			if ($row['ns'] <> 0) {
				$ns = number_format((($data['ns'] - $row['ns']) / $row['ns']) * 100, 2, '.', '') . "%";
			}
			
			if ($row['sj'] <> 0) {
				$sj = number_format((($data['sj'] - $row['sj']) / $row['sj']) * 100, 2, '.', '') . "%";
			}
			
			$data['per'] = array("d2"=>$d2, "t1"=>$t1, "t2"=>$t2, "ns"=>$ns, "sj"=>$sj);
			
			$avg = array();
			if ($d2 < 0) {
				$avg['d2'] = 'ui-icon-arrow-1-s';
			} else if ($d2 == 0) {
				$avg['d2'] = 'ui-icon-arrow-1-e';
			} else {
				$avg['d2'] = 'ui-icon-arrow-1-n';
			}
			
			if ($t1 < 0) {
				$avg['t1'] = 'ui-icon-arrow-1-s';
			} else if ($t1 == 0) {
				$avg['t1'] = 'ui-icon-arrow-1-e';
			} else {
				$avg['t1'] = 'ui-icon-arrow-1-n';
			}
			
			if ($t2 < 0) {
				$avg['t2'] = 'ui-icon-arrow-1-s';
			} else if ($t2 == 0) {
				$avg['t2'] = 'ui-icon-arrow-1-e';
			} else {
				$avg['t2'] = 'ui-icon-arrow-1-n';
			}
			
			if ($ns < 0) {
				$avg['ns'] = 'ui-icon-arrow-1-s';
			} else if ($ns == 0) {
				$avg['ns'] = 'ui-icon-arrow-1-e';
			} else {
				$avg['ns'] = 'ui-icon-arrow-1-n';
			}
			
			if ($sj < 0) {
				$avg['sj'] = 'ui-icon-arrow-1-s';
			} else if ($sj == 0) {
				$avg['sj'] = 'ui-icon-arrow-1-e';
			} else {
				$avg['sj'] = 'ui-icon-arrow-1-n';
			}
			
			$data['avg'] = $avg;
			
			/*
			if (is_object($mc)) {
				$mc->set($key, $data, 0, _strtotime("1 day"));
			}*/

			// print_r($row);
			
			return $data;
		}
	
		return $data;
	}
	
	function avg($row) {
		$emp = $data['emp'];
		$key = sprintf("%s_avg_%s_%s", $this->tbl, $data['emp'], $data['id']);
	
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
	
		$ct = strtotime($data['createtime']);
	
		$dd = date('Y-m-d', strtotime("-200 day", $ct));
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT avg(d2) d2, avg(t1) t1, avg(t2) t2, avg(ns) ns, avg(sj) sj FROM %s WHERE emp = '%s' AND createtime > '%s' AND createtime < '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $dd, $data['createtime']);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
	
		if (is_object($mc) && count($row) > 0) {
			$mc->set($key, $row, 0, _strtotime("1 day"));
		}
	
		return $row;
	}

	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$row = $this->id($this->tbl, $id);

			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/salary/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function finish($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'finance/salary/finish');
		return true;
	}

	function finishPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$email = $request['email'];
		$content = addslashes($request['content']);
		$status = 4;

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$row = $this->_view($id);
		if ($email == '1') {
			$from = $_SESSION['id'];

			$to = $row['emp'];
			$row['to'] = Name::emp($to);

			$controller->assign_by_ref('result', $row);

			$subject = sprintf('%s %s', $row['to'], $row['name']);
			$body = $controller->sunxy($request, 'finance/salary/body_5');
			// debug($body);

			$this->_notice($from, $to, $subject, $body);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_begin($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'finance/salary/batch_begin');
		return true;
	}
	
	function batch_finish($controller, $request) {
		$this->pretreat($controller);
				
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'finance/salary/batch_finish');
		return true;
	}
	
	function batch_file($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$controller->assign_by_ref('file_option', $this->_file());
	
		$controller->display($request, 'finance/salary/batch_file');
		return true;
	}
	
	function _file() {
		require_once(dirname(__FILE__) . '/SalaryFile.class.php');
		$aa = new SalaryFile();
	
		return $aa->_list();
	}
	
	function batch_beginPost($controller, $request) {
		return $this->batch_verifyPost($controller, $request);
	}
	
	function batch_verifyPost($controller, $request) {
		$this->pretreat($controller);
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_finishPost($controller, $request) {
		$this->pretreat($controller);
		
		while (list($k, $id) = each($request['cc'])) {
			$this->_finish22($controller, $request, $id);
		}		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _finish22($controller, $request, $id) {
		$finish = $request['finish'];
		$content = addslashes($request['content']);
		$status = 4;
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET finish = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $finish, $content, $status, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function batch_filePost($controller, $request) {
		$this->pretreat($controller);
	
		$file = $request['file'];
		
		require_once(dirname(__FILE__) . '/SalaryFile.class.php');
		$aa = new SalaryFile();
		$row = $aa->_view($file);
		
		$name = $row['name'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET file = '%s', name = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $file, $name, $content, $status, $id);
			debug($sql);
			$db->query($sql);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '实发',	
			'2' => '对私',
			'3' => '对公',			
		);
		$controller->assign('num_option', $num);
		
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if ($num_s == '3') {
			$fmt = "SELECT %s AS a, SUM(sj) AS c FROM %s a ";
		} elseif ($num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(ns) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, SUM(t2) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {		
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _emp_name($name) {
		$row = Common::name(Constant::tbl_emp, $name);

		return $row['id'];
	}
	
	function _emp_id_by_code($code) {
		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$aa = new Emp();
		
		$row = $aa->_emp_code($code);		
		if ($row) {
			return $row['id'];
		}
		
		return 0;
	}

	function z($controller, $request) {
		echo "\n" . $_REQUEST['do'] . "\n";

		$s = '" 10,027 "';
		

		logdate($controller, $request);
		return true;
	}
	
	function _list($id, $status = 'all') {
		$ret = array();
	
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT * FROM %s WHERE `file` = '%s' AND visible = '1' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id);
		} else {
			$fmt = "SELECT * FROM %s WHERE `file` = '%s' AND visible = '1' AND status = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id, $status);
		}
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$row['ok'] = true;
	
			$ret[] = $row;
		}
	
		return $ret;
	}
	
	function _amount($field = 'file', $id, $status = 'all') {
	
		$ff = "SUM(ns) ns, SUM(sj) sj, SUM(t1) t1, SUM(t2) t2";
		
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT %s FROM %s WHERE `%s` = '%s' AND visible = '1' ORDER BY id";
			$sql = sprintf($fmt, $ff, $this->tbl, $field, $id);
		} else {
			$fmt = "SELECT %s FROM %s WHERE `%s` = '%s' AND visible = '1' AND status = '%s' ORDER BY id";
			$sql = sprintf($fmt, $ff, $this->tbl, $field, $id, $status);
		}
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
	
		return $row;
	}
	
	function file($controller, $request) {
		
		require_once(dirname(__FILE__) . '/SalaryFile.class.php');
		$aa = new SalaryFile();
	
		$dd = date("Y-m-d", strtotime("-3 day"));
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ORDER BY id";
		// $fmt = "SELECT * FROM %s ORDER BY ID DESC LIMIT 10";
		// $fmt = "SELECT * FROM %s WHERE createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
	
		$name = '';
		while ($row = $db->fetch_array($result)) {
			
			if (!empty($name) && $name <> $row['name'] && $office <> $row['office']) {
				$row['date'] = $row['createtime'];
				$row['emp'] = $row['emp'];
				
				$id = $aa->_insert($row);
				
				$fmt = "UPDATE %s SET file = '%s' WHERE file is NULL AND name = '%s' AND office = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id, $name, $office);
				echo $sql . "\n";
				$db->query($sql);
				
				echo "I\n";
			}
			
			$office = $row['office'];
			$name = $row['name'];
			
			echo ".";
		}
	
		echo "\n" . $_REQUEST['do'] . "\n";
	
		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$db = newdb($this->dsn_w);
		
		$dd = date("Y-m-d", strtotime("-3 day"));
		
		$fmt = "SELECT * FROM %s WHERE id = 13572";
		// $fmt = "SELECT * FROM %s ORDER BY ID DESC LIMIT 10";
		$fmt = "SELECT * FROM %s WHERE createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		while ($row = $db->fetch_array($result)) {
			// print_r($row);
			
			// echo $row['sj_x'];
			
			$dfgz = $this->_dfgz($row['sj_x']);
			
			// echo $dfgz . "\n";
			
			$fmt = "UPDATE %s SET dfgz = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $dfgz, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _dfgz($sj_x) {
		// debug(sprintf("sj_x = %s\n", $sj_x));
		
		if (empty($sj_x)) {
			return 0;
		}
		
		$a = json_decode($sj_x);
		
		// print_r($a);
		
		$s = $a->bank;
		
		if (preg_match('/^中国工商银行/', $s)) {
			return 1;
		} elseif (preg_match('/^中信银行/', $s)) {
			return 2;
		} elseif (preg_match('/^东莞农村商业银行/', $s)) {
			return 3;
		} elseif (preg_match('/^中国民生银行/', $s)) {
			return 4;
		} 
		
		return 9;
		
	}

	function zh($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE ns_x is NULL";
		$fmt = "SELECT * FROM %s WHERE id = 1297";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$payment_x = $this->_payment_x($row['emp'], '1');

			$fmt = "UPDATE %s SET ns_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $payment_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function gh($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		// $fmt = "SELECT * FROM %s WHERE sj_x is NULL";
		// $fmt = "SELECT * FROM %s WHERE createtime > '2011-05-30'";
		$fmt = "SELECT * FROM %s WHERE id = 1297";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$payment_x = $this->_payment_x($row['emp'], '2');

			$fmt = "UPDATE %s SET sj_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $payment_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function sj($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/sj1104a.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}

			$a = preg_split("/\t/", $buf);
			$no = trim($a[0]);

			if (!preg_match("/^\d+$/", $no)) {
				continue;
			}

			print_r($a);
			// echo $a[1] . "\n";
			$d1 = _trim($a[9]);
			$d2 = _trim($a[10]);

			//$d3 = _trim($a[4]);
			//$d4 = _trim($a[5]);
			//$d5 = _trim($a[6]);

			$s1 = _trim($a[17]);
			// $s2 = _trim($a[20]);
			$s3 = _trim($a[20]);
			//  $s4 = _trim($a[10]);

			// $k1 = _trim($a[12]);
			// $k2 = _trim($a[13]);
			// $k9 = _trim($a[14]);

			$t1 = _trim($a[26]);
			$kd = _trim($a[43]);
			$t2 = _trim($a[38]);

			$ns = _trim($a[45]);
			$sj = _trim($a[44]);

			$name = _trim($a[1]);

			$emp = $this->_emp_name($name);

			$fmt = "INSERT INTO %s(name, emp, d1, d2, s1, s3, t1, t2, kd, ns, sj, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $name, $emp, $d1, $d2, $s1, $s3, $t1, $t2, $kd, $ns, $sj);
			echo $sql . "\n";
			$db->query($sql);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function _payment_x($emp, $tec) {
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();

		$bank = $aa->_bank_salary($emp, $tec);

		$ret['bank'] = $bank['bank'] . $bank['branch'];
		$ret['name'] = $bank['name'];
		$ret['code'] = $bank['code'];
		$ret['content'] = '';

		$ret = json_encode($ret);

		return addslashes($ret);
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/salary/_list_c');
		return true;
	}
	
	function baitiao($controller, $request) {
		
		$request['tpl'] = "xls";
		
		$name = date("Y年m月工资", strtotime("-1 month"));
		
		$data = array();
		for ($i = 0; $i < 21; $i++) {
			$row['name'] = $name;
			
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		
		// $filename = sprintf("%s.xls", date("YmdHis"));
		$filename = 'baitiao.xls';
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'finance/salary/baitiao');
		return true;
	}
	
	function ccc($controller, $request) {
		echo $this->alpha2num('a');
	
		echo "\n" . $_REQUEST['do'] . "\n";
	
		logdate($controller, $request);
		return true;
	}
	
	
}

function _trim($s) {
	$s = trim($s);
	if (empty($s)) {
		return '';
	}

	$s = preg_replace("/[\s|\"|\'|,|-]+/i", "", $s);

	return $s;
}
?>